<?php

	namespace App\Http\Controllers;

	use Illuminate\Http\Request;
	use App\Http\Requests;
	use App\Http\Controllers\Controller;
	use Illuminate\Queue\Capsule\Manager;
	use Session;
	use DB;
	use Illuminate\Support\Facades\Input;
	use Illuminate\Support\Facades\Redirect;
	use Illuminate\Support\Facades\Log;
	use Illuminate\Support\Facades\Auth;
	use App\Order;
	use App\BankShop;
	use App\StateConfig;
	use App\ServeProject;

	class OrderController extends Controller
	{
		public function __construct()
		{
			$this->middleware('auth');
		}

		public function index(Request $request) {
			// 查看当前菜单可使用的功能按钮
			$uri = $request->path();
			$data['optionArr'] =  $this->checkMenuOption($uri);

			$order_noS = '';
			$shopS = '';
			$stateS = '';
			$serveS = '';
			if($request->input('order_noS')){
				$order_noS = trim($request->input('order_noS'));
			}

			if($request->input('stateS')){
				$stateS = trim($request->input('stateS'));
			}
			if($request->input('serveS')){
				$serveS = trim($request->input('serveS'));
			}

			/**
			 *  判断当前登录账号，如果是第三方网点公司账号，则查询显示当前网点的订单
			 *  如果不是，则默认显示所有网点订单
			 */
			$adminUser = Auth::user();
			$phone = $adminUser->name;
			$bank_id = BankShop::getIdByPhone($phone);
			if($bank_id){
				// 网点存在，查询显示对应网点的订单
				$shopS = $bank_id;
				$data['bankShop'] = BankShop::getTheOneBankShopId($bank_id);
//				dd($data['bankShop']);
			}else{
				// 全部网点
				if($request->input('shopS')){
					$shopS = trim($request->input('shopS'));
				}
				$data['bankShop'] = BankShop::getAllBankShopId(); // 获取所有店铺
//				dd($data['bankShop']);
			}

			// 订单查询
			$data['orderList'] = Order::getAllOrderList($order_noS,$shopS,$stateS,$serveS);//所有订单
			$data['orderSate'] = StateConfig::getAllState('order');// 获取订单的状态
			$data['serveProject'] = ServeProject::getAllServeId(); // 获取服务项目
//			dd($data['orderList']);
			//search
			$data['order_noS'] = $order_noS;
			$data['shopS'] = $shopS;
			$data['stateS'] = $stateS;
			$data['serveS'] = $serveS;

			return view('order.list')->with($data);
		}

		public function show($id)
		{
			$orderInfo = Order::getOrderInfo($id);// 订单信息
//			$orderServe = Order::getOrderServe($id); // 服务
			$orderUse = Order::getOrderUseHistory($id); // 服务
			$orderRefund = Order::getOrderFund($id); // 退款
			if($orderRefund){
				if($orderRefund->check_text){
					$orderRefund->check_text = explode(',',$orderRefund->check_text);
				}
			}
//			dd($orderUse);
			$data['orderInfo'] = $orderInfo;
//			$data['orderServeInfo'] = $orderServe;
			$data['orderUseHistory'] = $orderUse;
			$data['orderFundInfo'] = $orderRefund;

			return view('order.show')->with($data);

		}

		/**
		 * 订单数据统计
		 */
		public function orderDataStatistics(Request $request){
			// 下单量   、下单总额   退单量、退单总额    收益
			$where=array();
			$shopS = '';
			$e_codeS = '';
			// 日期
			if($request->validity){
				$choseTime = $request->validity;
				$validityArr = explode(' - ',$choseTime);
				$where['startTime'] = $validityArr[0];
				$where['endTime'] = $validityArr[1];
			}
			/**
			 *  判断当前登录账号，如果是第三方网点公司账号，则查询显示当前网点的订单
			 *  如果不是，则默认显示所有网点订单
			 */
			$adminUser = Auth::user();
			$phone = $adminUser->name;
			$bank_id = BankShop::getIdByPhone($phone);
			if($bank_id){
				// 网点存在，查询显示对应网点的订单
				$shopS = $bank_id;
				$data['bankShop'] = BankShop::getTheOneBankShopId($bank_id);
			}else{
				// 全部网点
				if($request->input('shopS')){
					$shopS = trim($request->input('shopS'));
				}
				$data['bankShop'] = BankShop::getAllBankShopId(); // 获取所有网点
			}
//				dd($data['bankShop']);


			// 设备编号
			if($request->input('e_codeS')){
				$e_codeS = strtoupper(trim($request->input('e_codeS')));
			}
			$ordersList = Order::getOrdersEverydayTotal($where,$shopS,$e_codeS);
			//			dd($ordersList);

			$orders = array();
			$dataArr = array(); // 日期数组
			$xdArr = array();   // 下单量数组
			$thArr = array();   // 退单量数组

			if($ordersList){
				$dateT = array(); // 折线图数据：开始、结束 日期数组
				$xdTotalArr = array(); // 折线图数据：获取数
				$thTotalArr = array(); // 折线图数据：消耗数
				foreach($ordersList as $key=>$value){
					$orders[$key]['times'] = $value->times;
					$orders[$key]['bank_id'] = $value->bank_id;
					$orders[$key]['e_code'] = $value->e_code;
					$orders[$key]['xdTotal'] = isset($value->xdTotal) ? $value->xdTotal: 0;   // 下单量
					$orders[$key]['xdTotalMoney'] = isset($value->xdTotalMoney) ? $value->xdTotalMoney : 0; //下单总金额
					$orders[$key]['thTotal'] = isset($value->thTotal) ? $value->thTotal : 0;   // 退货量
					$orders[$key]['thTotalMoney'] = isset($value->thTotalMoney) ? $value->thTotalMoney : 0; // 退货总金额
					$yue = $value->xdTotalMoney - $value->thTotalMoney; // 收益小计
					if($yue < 0){
						$orders[$key]['yue'] = '亏损'.abs($yue);
					}else{
						$orders[$key]['yue'] = $yue;
					}

					// 获取时间数据：当前页两个时间
					if($key == 0){
						$dateT[] =  $value->times;
					}
					if($key == count($ordersList)-1){
						$dateT[] =  $value->times;
					}
					// 拼接获取数、消耗数 相关数组
					$xdTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
					$xdTotalArr[$key]['xdTotal'] = isset($value->xdTotal) ? $value->xdTotal : 0;   // 下单量
					$thTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
					$thTotalArr[$key]['thTotal'] = isset($value->thTotal) ? $value->thTotal : 0;   // 退货量
				}
				//			dd($thTotalArr);
				// 线形图数据---------------------------------------------------------------------------
				$curent_week = array();
				// 根据查询的结果数据组装相关参数值
				// 判断是否用了时间检索
				if($where){
					// 按照检索项目查找
					$i_ct = round((strtotime($where['endTime'])-strtotime($where['startTime']))/3600/24); // 计算开始到结束之间的天数
					// 拼接数组
					for($i=0;$i<=$i_ct;$i++){
						if($i==0){
							$curent_week[$i] = $where['startTime'];
						}
						$curent_week[$i] = date('Y-m-d',strtotime($where['startTime']."+".$i." day"));
					}
				}else{
					$start = $dateT[0];
					$end = $dateT[1];
					$i_ct = round((strtotime($end)-strtotime($start))/3600/24); // 计算开始到结束之间的天数
					// 时间数据值
					for($i=0;$i<=$i_ct;$i++){
						if($i==0){
							$curent_week[$i] = $dateT[0];
						}
						$curent_week[$i] = date('Y-m-d',strtotime($dateT[0]."+".$i." day"));
					}
				}
				//dd($curent_week);
				// 整理 日期、获取数、消耗数 的数组
				if($curent_week){
					foreach ($curent_week as $key=>$value) {
						$dataArr[] = str_replace('-', '月', substr($value, strpos($value, '-') + 1));
					}
					// 组装数据
					foreach ($curent_week as $key=>$value){
						// 下单量数组
						for($i=0;$i<count($xdTotalArr);$i++){
							if(strtotime($value)==strtotime($xdTotalArr[$i]['times'])){
								$xdArr[$key] = (int)$xdTotalArr[$i]['xdTotal'];
								break;
							}else{
								$xdArr[$key]= 0;
							}
						}
						// 退货量数组
						for($i=0;$i<count($thTotalArr);$i++){
							if(strtotime($value)==strtotime($thTotalArr[$i]['times'])){
								$thArr[$key] = (int)$thTotalArr[$i]['thTotal'];
								break;
							}else{
								$thArr[$key]= 0;
							}
						}
					}
				}
			}

			$dataArr = json_encode($dataArr);
			$xdArr = json_encode($xdArr);
			$thArr = json_encode($thArr);
			$data['dataArr'] = $dataArr;
			$data['xdArr'] = $xdArr;
			$data['thArr'] = $thArr;
			$data['orderStatistics'] = $orders;
//			dd($orders);
			$data['validity'] = $request->validity;
			$data['shopS'] = $shopS;
			$data['e_codeS'] = $request->input('e_codeS');
			return view('order.order_statistic')->with($data);
		}



		// 财务数据统计
		public function orderMoneyStatistics(Request $request){
			$where=array();
			$shopS = '';
			// 日期
			if($request->validity){
				$choseTime = $request->validity;
				$validityArr = explode(' - ',$choseTime);
				$where['startTime'] = $validityArr[0];
				$where['endTime'] = $validityArr[1];
			}
			/**
			 *  判断当前登录账号，如果是第三方网点公司账号，则查询显示当前网点的订单
			 *  如果不是，则默认显示所有网点订单
			 */
			$adminUser = Auth::user();
			$phone = $adminUser->name;
			$bank_id = BankShop::getIdByPhone($phone);
			if($bank_id){
				// 网点存在，查询显示对应网点的订单
				$shopS = $bank_id;
				$data['bankShop'] = BankShop::getTheOneBankShopId($bank_id);
			}else{
				// 全部网点
				if($request->input('shopS')){
					$shopS = trim($request->input('shopS'));
				}
				$data['bankShop'] = BankShop::getAllBankShopId(); // 获取所有网点
			}
//				dd($data['bankShop']);


			$orders = array();
			$dataArr = array(); // 日期数组
			$xdArr = array();   // 下单量数组
			$thArr = array();   // 退单量数组

			$orderMoneyList = Order::getOrdersMoneyTotal($where,$shopS);
//			dd($orderMoneyList);
			if($orderMoneyList){
				$dateT = array(); // 折线图数据：开始、结束 日期数组
				$xdTotalArr = array(); // 折线图数据：获取数
				$thTotalArr = array(); // 折线图数据：消耗数
				foreach($orderMoneyList as $key=>$value){
					$orders[$key]['bank_id'] = $value->bank_id;     // 2018 3.13 add
					$orders[$key]['times'] = $value->times;
					$orders[$key]['yfMoney'] = isset($value->yfMoney) ? $value->yfMoney: 0;   // 应付
					$orders[$key]['sfMoney'] = isset($value->sfMoney) ? $value->sfMoney : 0; //实付
					$orders[$key]['dcTotalMoney'] = isset($value->dcTotalMoney) ? $value->dcTotalMoney : 0;   // 优惠
					$orders[$key]['yue'] = isset($value->sfMoney) ? $value->sfMoney : 0;  // 收益小计

					// 获取时间数据：当前页两个时间
					if($key == 0){
						$dateT[] =  $value->times;
					}
					if($key == count($orderMoneyList)-1){
						$dateT[] =  $value->times;
					}
					// 拼接获取数、消耗数 相关数组
					$xdTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
					$xdTotalArr[$key]['yfMoney'] = isset($value->yfMoney) ? $value->yfMoney : 0;   // 应收
					$thTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
					$thTotalArr[$key]['sfMoney'] = isset($value->sfMoney) ? $value->sfMoney : 0;   // 实收
				}
				//			dd($thTotalArr);
				// 线形图数据---------------------------------------------------------------------------
				$curent_week = array();
				// 根据查询的结果数据组装相关参数值
				// 判断是否用了时间检索
				if($where){
					// 按照检索项目查找
					$i_ct = round((strtotime($where['endTime'])-strtotime($where['startTime']))/3600/24); // 计算开始到结束之间的天数
					// 拼接数组
					for($i=0;$i<=$i_ct;$i++){
						if($i==0){
							$curent_week[$i] = $where['startTime'];
						}
						$curent_week[$i] = date('Y-m-d',strtotime($where['startTime']."+".$i." day"));
					}
				}else{
					$start = $dateT[0];
					$end = $dateT[1];
					$i_ct = round((strtotime($end)-strtotime($start))/3600/24); // 计算开始到结束之间的天数
					// 时间数据值
					for($i=0;$i<=$i_ct;$i++){
						if($i==0){
							$curent_week[$i] = $dateT[0];
						}
						$curent_week[$i] = date('Y-m-d',strtotime($dateT[0]."+".$i." day"));
					}
				}
				//dd($curent_week);
				// 整理 日期、获取数、消耗数 的数组
				if($curent_week){
					foreach ($curent_week as $key=>$value) {
						$dataArr[] = str_replace('-', '月', substr($value, strpos($value, '-') + 1));
					}
					// 组装数据
					foreach ($curent_week as $key=>$value){
						// 下单量数组
						for($i=0;$i<count($xdTotalArr);$i++){
							if(strtotime($value)==strtotime($xdTotalArr[$i]['times'])){
								$xdArr[$key] = (int)$xdTotalArr[$i]['yfMoney'];
								break;
							}else{
								$xdArr[$key]= 0;
							}
						}
						// 退货量数组
						for($i=0;$i<count($thTotalArr);$i++){
							if(strtotime($value)==strtotime($thTotalArr[$i]['times'])){
								$thArr[$key] = (int)$thTotalArr[$i]['sfMoney'];
								break;
							}else{
								$thArr[$key]= 0;
							}
						}
					}
				}
			}
			$dataArr = json_encode($dataArr);
			$xdArr = json_encode($xdArr);
			$thArr = json_encode($thArr);
			$data['dataArr'] = $dataArr;
			$data['xdArr'] = $xdArr;
			$data['thArr'] = $thArr;
			$data['orderStatistics'] = $orders;
//			dd($orders);
			$data['validity'] = $request->validity;
			$data['shopS'] = $shopS;

			return view('order.money_statistic')->with($data);
		}


	}





